package com.ui.basic.controller.admin;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.CommandUser;
import com.ui.basic.service.admin.UserService;

@Controller
public class SetupUserController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(SetupUserController.class);

	@Autowired
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/admin/user.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>SetupUserController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "user.page";
	}
	
	@RequestMapping(value = "/admin/user.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandUser") CommandUser commandUser) {
		LOGGER.debug(">>>SetupUserController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			userService.setComboData(commandUser);
			model.addAttribute("commandUser", userService.getUser(commandUser));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "user.view";
	}

	@RequestMapping(value = "/admin/user.form", method = RequestMethod.POST)
	public String showForm(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandUser") CommandUser commandUser) {
		LOGGER.debug(">>>SetupUserController.showForm");
		return edit(model, request, response, commandUser);
	}

	@RequestMapping(value = "/admin/user.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandUser") CommandUser commandUser) {
		LOGGER.debug(">>>SetupUserController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			userService.setComboData(commandUser);

			if (commandUser.getId() != null)
				model.addAttribute("commandUser", userService
						.getUserDetail(commandUser));
			else {
				model.addAttribute("commandUser", commandUser);
				commandUser.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "user.edit";
	}

	@RequestMapping(value = "/admin/user.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandUser") CommandUser commandUser,
			BindingResult result, Model model) {
		LOGGER.debug(">>>SetupUserController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandUser, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				userService.save(commandUser);
				return view(model, request, response, commandUser);
			} catch (Exception e) {
				if (commandUser.getId() != null)
					sendError(response, getMessage(request,
							"error.userdata.saveFailed",
							new String[] { commandUser.getUserId() }));
				else
					sendError(response, getMessage(request,
							"error.userdata.exist", new String[] { commandUser
									.getUserId() }));
				return null;
			}
		}
		return null;
	}

	@RequestMapping(value = "/admin/user.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandUser") CommandUser commandUser,
			BindingResult result) {
		LOGGER.debug(">>>SetupUserController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandUser.getId() != null
				|| (commandUser.getSelectedRecord() != null && commandUser
						.getSelectedRecord().length > 0)) {
			try {
				userService.delete(commandUser);
				return view(model, request, response, commandUser);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}
}